package cn.edu.zafu.bigdata.llc;

import java.io.*;
import java.util.*;

public class StudentInfoSystem {

    private static final String FILE_NAME = "students.dat";




    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Student> students = loadStudents();

        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1 -> addStudent(students);
                case 2 -> findStudent(students);
                case 3 -> updateStudent(students);
                case 4 -> deleteStudent(students);
                case 5 -> {
                    saveStudents(students);
                    System.out.println("程序已退出。");
                    System.exit(0);
                }
                default -> System.out.println("无效的选项，请重新输入。");
            }
        }
    }

    private static void addStudent(List<Student> students) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.println("学号重复，请重新输入。");
                return;
            }
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.add(student);
        System.out.println("学生信息添加成功。");
    }

    private static void findStudent(List<Student> students) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号进行检索: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = findStudentById(students, id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号进行检索或输入QUIT退出: ");
            id = scanner.nextLine();
        }
    }

    private static void updateStudent(List<Student> students) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号进行更新: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = findStudentById(students, id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号进行更新或输入' QUIT '退出: ");
            id = scanner.nextLine();
        }
    }

    private static void deleteStudent(List<Student> students) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号进行删除: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = findStudentById(students, id);
            if (student != null) {
                students.remove(student);
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号进行删除或输入' QUIT '退出: ");
            id = scanner.nextLine();
        }
    }

    private static Student findStudentById(List<Student> students, String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    private static List<Student> loadStudents() {
        List<Student> students = new ArrayList<>();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            Object obj = ois.readObject();
            // 显式地进行类型检查和转换
            if (obj instanceof List) {
                students = (List<Student>) obj;
            } else {
                System.out.println("读取的对象不是预期的List<Student>类型");
                // 这里可以抛出一个异常或者处理错误情况
            }
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("无法加载学生信息，将创建新的列表。");
            e.printStackTrace(); // 打印堆栈跟踪以便于调试
        }
        return students;
    }


    private static void saveStudents(List<Student> students) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.out.println("无法保存学生信息。");
        }

    }
}